﻿(function (window, undefined) {

    var $ = window.$, app = window.app;
    app.TreeList = function () {
        var self = this;
        self.selectValueControl = "";
        self.displayField = "";
        self.valueField = "";
        self.onBodyDown = function (event) {
            if (!(event.target.id === self.selectValueControl + "MenuContent" ||
                $(event.target).parents("#" + self.selectValueControl + "MenuContent").length > 0)) {
                self.hideMenu();
            }
        };
        self.showMenu = function (treeSel) {
            $("#" + self.selectValueControl + "MenuContent").show();
            $("body").bind("mousedown", self.onBodyDown);
        };
        self.hideMenu = function () {
            $("#" + self.selectValueControl + "MenuContent").hide();
            $("body").unbind("mousedown", self.onBodyDown);
        };
        self.init = function (zNodeData, selectValueControl, selectValue, displayField, valueField) {
            self.selectValueControl = selectValueControl;
            self.displayField = displayField;
            self.valueField = valueField;
            $.fn.zTree.init($("#" + self.selectValueControl + "_Treemenu"),
                {
                    view: { dblClickExpand: false },
                    data: { simpleData: { enable: true } },
                    callback: {
                        onClick: function (e, treeId, treeNode) {
                            var $selectValueTextControl = $("#" + self.selectValueControl + "Text");
                            var $selectValueControl = $("#" + selectValueControl);

                            if (self.displayField == null) {
                                self.displayField = "";
                                self.valueField = "";
                            }


                            if (self.displayField === "")
                                $selectValueTextControl.val(treeNode.name);
                            else
                                $selectValueTextControl.val(treeNode.raw[self.displayField]);


                            if (self.valueField === "")
                                $selectValueControl.val(treeNode.id);
                            else
                                $selectValueControl.val(treeNode.raw[self.valueField]);

                            //触发控件的change事件
                            $selectValueTextControl.change();
                            $selectValueControl.change();
                            //执行验证
                            $selectValueTextControl.valid();
                            $selectValueControl.valid();

                            self.hideMenu();
                        }
                    }
                },
                zNodeData);

            $("#" + self.selectValueControl + "Trigger").click(function () {
                self.showMenu(this);
            });

            var treeObj = $.fn.zTree.getZTreeObj(self.selectValueControl + "_Treemenu");
            //var node = treeObj.getNodeByParam( self.valueField, selectValue, null);


            var nodes = treeObj.transformToArray(treeObj.getNodes());
            var node = null;
            for (var i = 0; i < nodes.length; i++) {
                if (self.valueField != ""&&  nodes[i].raw[self.valueField] === selectValue) {
                    node = nodes[i];
                    break;
                }
                //兼容旧的
                if (nodes[i].id === selectValue) {
                    node = nodes[i];
                    break;
                }
            }
            if (node) {
                treeObj.selectNode(node);


                if (self.displayField === "")
                    $("#" + self.selectValueControl + "Text").val(node.name);
                else
                    $("#" + self.selectValueControl + "Text").val(node.raw[self.displayField]);
            }
        };
    };

    app.SelectBox = function () {
        var self = this;
        self.selectValueControl = "";

        self.init = function (selectValueControl, selectValue) {
            self.selectValueControl = selectValueControl;
            //$("#" + self.selectValueControl+"Text").val(selectValue);
            //$("#" + self.selectValueControl).val(selectValue);

        };

    };
    app.ImageUpload = function () {
        var self = this;
        self.selectValueControl = "";

        self.init = function (selectValueControl, selectValue) {
            self.selectValueControl = selectValueControl;

            $("#thumbnail" + self.selectValueControl).attr('src', selectValue);
            $("#" + self.selectValueControl).val(selectValue);

            $("#" + self.selectValueControl + "FileUpload").change(function () {
                var file = this.files;
                var windowUrl = window.URL || window.webkitURL;
                for (let i = 0; i < file.length; i++) {
                    //限制图片大小
                    //if (file[i].size > 1024 * 1024 * 2) {
                    //    alert('图片大小不能超过 2MB!');
                    //}
                    //预览图片
                    var dataUrl = windowUrl.createObjectURL(file[i]);
                    $("#thumbnail" + self.selectValueControl).attr('src', dataUrl);

                    $("#" + self.selectValueControl).val(dataUrl);
                }
            });
        };
    };
}(window));